//
//  HCSureViewController.m
//  Aierle
//
//  Created by WillyZhao on 2017/6/3.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCSureViewController.h"

#import "WayModelDateView.h"

#import "HCAppointmentDetailModel.h"

#import "HCSureSuccess.h"


@interface HCSureViewController () {
    UIView *homeView;
    UIView *topView;
    UIView *midView;
    UIView *downVeiw;
    UIButton *women_button;
    UIButton *men_button;
    UIButton *sure_button;
    
    NSMutableArray *arr_hh;
    NSArray *arr_des;
    UILabel *personLabel;   //人数label
    WayModelDateView *dateViewp;
    UILabel *timeLabel;     //时间label
    WayModelDateView *dateViewt;
    
    WayModelDateView *dateViewx;
    UILabel *noteLabel;     //备注label
    
    NSString *isSex;        //区别性别
    
    UITextField *nameField;     //名称
    UITextField *mobileField;   //手机号码
    
    
    HCAppointmentDetailModel *model;
}

@property (nonatomic, strong) HCSureSuccess *draftView;


@end

@implementation HCSureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    model = (HCAppointmentDetailModel *)self.parameterObject;
    
    self.textTitle = [NSString stringWithFormat:@"%@",model.name];

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkPerson:) name:@"person" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTime:) name:@"time" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNote:) name:@"note" object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //视图初始化
    [self inlinazieViews];
    
//    self.navigationItem.titleView = [WayBrandReservationViewController changeNavigationTitleByFontSize:self.parameterObject];
    
    self.dataRequest.delegate = self;
    arr_hh = [NSMutableArray array];
    for (int i = 1; i<12; i++) {
        [arr_hh addObject:[NSString stringWithFormat:@"%d人",i]];
    }
    arr_des = [NSArray arrayWithObjects:@"需要特殊照顾",@"需要非毛绒场所",@"需要安静",@"需要靠窗位置", nil];

    
    // Do any additional setup after loading the view.
}

-(void)checkPerson:(NSNotification *)na {
    personLabel.text = [NSString stringWithFormat:@"%@",na.userInfo[@"person"]];
    [dateViewp dismiss];
}

-(void)checkTime:(NSNotification *)na {
    timeLabel.text = [NSString stringWithFormat:@"%@",na.userInfo[@"time"]];
    [dateViewt dismiss];
}

-(void)checkNote:(NSNotification *)na {
    noteLabel.text = [NSString stringWithFormat:@"%@",na.userInfo[@"note"]];
    [dateViewx dismiss];
}

#pragma mark 人数选择
-(void)personCount {
    [self.view endEditing:YES];
    dateViewp = [[WayModelDateView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-200, DeviceWidth, 200) AndStyle:WayModelPickerViewOneSection WithData:arr_hh];
    dateViewp.sure_block = ^(NSString *str){
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:str forKey:@"person"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"person" object:nil userInfo:dic];
    };
    [dateViewp show];
    
}

#pragma mark 时间选择
-(void)timeSelect {
    [self.view endEditing:YES];
    dateViewt = [[WayModelDateView alloc] initWithFrame:CGRectMake(0, DeviceHeight-200, DeviceWidth, 200) AndStyle:WayModelPickerViewMoreSection WithData:arr_hh];
    dateViewt.sure_block = ^(NSString *str){
        if ([PublicFunction isNull:str]) {
            NSDate *datea = [NSDate date];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
            NSString *str = [dateFormatter stringFromDate:datea];
            NSString *time = [NSString stringWithFormat:@"%@0点0分",str];
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:time forKey:@"time"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"time" object:nil userInfo:dic];
        }else{
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:str forKey:@"time"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"time" object:nil userInfo:dic];
        }
    };
    [dateViewt show];
}

#pragma mark 备注选择
-(void)noteSelect {
    [self.view endEditing:YES];
    dateViewx = [[WayModelDateView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-200, DeviceWidth, 200) AndStyle:WayModelPickerViewOneSection WithData:arr_des];
    dateViewx.sure_block = ^(NSString *str){
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:str forKey:@"note"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"note" object:nil userInfo:dic];
    };
    [dateViewx show];
}

#pragma mark - 初始化

-(void)inlinazieViews {
//    BASE_URL
    homeView = [[[NSBundle mainBundle] loadNibNamed:@"dingzuo" owner:self options:nil]firstObject];
    homeView.frame = CGRectMake(0, 0, DeviceWidth, self.centerView.frame.size.height);
    homeView.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    [self.centerView addSubview:homeView];
    
    women_button = (UIButton *)[homeView viewWithTag:101];
    men_button = (UIButton *)[homeView viewWithTag:102];
    sure_button = (UIButton *)[homeView viewWithTag:103];
    sure_button.layer.cornerRadius = 10.0f;
    
    //人数选择
    UIView *vc_aa = (UIView *)[homeView viewWithTag:1031];
    //人数label
    personLabel = (UILabel *)[homeView viewWithTag:2045];
    
    //时间选择
    UIView *vc_bb = (UIView *)[homeView viewWithTag:10987];
    timeLabel  = (UILabel *)[homeView viewWithTag:20987];
    
    //备注选择
    UIView *vc_cc = (UIView *)[homeView viewWithTag:27289];
    noteLabel = (UILabel *)[homeView viewWithTag:37389];
    
    //名称
    nameField = (UITextField *)[homeView viewWithTag:7777];
    
    //手机号
    mobileField = (UITextField *)[homeView viewWithTag:77777];
    
    //添加人数选择点击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(personCount)];
    vc_aa.userInteractionEnabled = YES;
    [vc_aa addGestureRecognizer:tap];
    
    
    //添加时间选择点击
    UITapGestureRecognizer *tap_b = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeSelect)];
    vc_bb.userInteractionEnabled = YES;
    [vc_bb addGestureRecognizer:tap_b];
    
    //添加备注选择点击
    UITapGestureRecognizer *tap_c = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noteSelect)];
    vc_cc.userInteractionEnabled = YES;
    [vc_cc addGestureRecognizer:tap_c];
    
    [women_button addTarget:self action:@selector(women_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [men_button addTarget:self action:@selector(women_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [sure_button addTarget:self action:@selector(sure_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    women_button.tag = 1;
    men_button.tag = 2;
    sure_button.tag = 3;
    [sure_button setBackgroundColor:YBKRGBCOLOR(241, 113, 31, 1)];
    
    UIView *line = (UIView *)[homeView viewWithTag:10231];
    UIView *line1 = (UIView *)[homeView viewWithTag:10232];
    
    line.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    line1.backgroundColor = line.backgroundColor;

    
    [women_button setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    [women_button setBackgroundImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    
    [men_button setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    [men_button setBackgroundImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    
    
    timeLabel.text = [PublicFunction getDateFromNSString:[PublicFunction getDateNow]];
    
}

-(void)women_buttonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    
    /**
     ***isSex用于区别男性女性***
     ***0表示男性***
     ***1表示女性***
     **/
    
    if (button.tag == 1) {
        women_button.selected = YES;
        men_button.selected = NO;
        isSex = [NSString stringWithFormat:@"0"];
    }else{
        men_button.selected = YES;
        women_button.selected = NO;
        isSex = [NSString stringWithFormat:@"1"];
    }
    
}

-(void)sure_buttonAction:(id)sender {
    
    if (women_button.isSelected == NO&&men_button.isSelected == NO) {
        [XHToast showBottomWithText:@"请选择性别"];
        return;
    }
    
    if ([PublicFunction isNull:nameField.text]) {
            [XHToast showBottomWithText:@"请填写名称"];
            return;
        }
    if (![PublicFunction checkTelNumber:mobileField.text]) {
            [XHToast showBottomWithText:@"请填写正确手机号码"];
        return;
        }
            
            if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                [XHToast showBottomWithText:@"请先登录"];
            }else{
                [self.dataRequest postHC_appointmentSure:self.dataObject userid:HCGETUSER(@"userid") appoint_time:[PublicFunction getIntegerWithDate:timeLabel.text] appointment_text:noteLabel.text name:nameField.text phone:mobileField.text];
            }

}
- (HCSureSuccess *)draftView {
    
    if (!_draftView) {
        
        _draftView = [[HCSureSuccess alloc] initWithFrame:YBKAppFrame];
//        [self.view addSubview:_draftView];
    }
    return _draftView;
}

#pragma mark -- 网络请求返回
-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    
    if ([dictionary[@"stat"] integerValue] == 200) {
//        self.draftView.hidden = NO;
        [self.draftView showAnimateSuccess:YES block:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
}else{
    [self.draftView showAnimateSuccess:NO block:^{
        NSLog(@"预约失败");
    }];
    }

    NSLog(@"订座结果返回:%@",dictionary);
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"失败");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
